﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Data.Api.Repositories;
using Data.Db;
using System.Data.Entity;
using Data.Mappers;
using Data.ModelDatabase;

namespace Data.Repositories
{
    public class BasketRepository : IBasketRepository
    {
        protected readonly IDbShopConcreteContext context;
        protected readonly IMapper<Entity.Baskets.Basket, ModelDatabase.Basket> mapper;

        public BasketRepository(IDbShopConcreteContext context, IMapper<Entity.Baskets.Basket, ModelDatabase.Basket> mapper)
        {
            this.context = context;
            this.mapper = mapper;
        }

        public void AddNewOrder(Entity.Products.Product product, string username)
        {
            Data.ModelDatabase.User userDb = context.Users.Where(u => u.Username == username).Single();
            Data.ModelDatabase.Product productDb = context.Products.Find(product.Id);


            if(userDb.Basket.Where(b => b.StateValue == (int)State.NotOrdered).Count() == 0)
            {
                // create new basket
                userDb.Basket.Add(new Data.ModelDatabase.Basket() { State = State.NotOrdered, User = userDb });

                // add new order to basket
                Data.ModelDatabase.Basket basket = userDb.Basket.Where(b => b.StateValue == (int)State.NotOrdered).Single();
                basket.OrderPosition.Add(new OrderPosition() { Product = productDb, ProductId = productDb.Id, Count = 1 });
            }
            else
            {
                if (userDb.Basket.Where(b => b.StateValue == (int)State.NotOrdered).Single().OrderPosition.Select(i => i.ProductId).Contains(product.Id)) // count++
                {
                    userDb.Basket.Where(b => b.StateValue == (int)State.NotOrdered).Single().OrderPosition.Where(i => i.ProductId == product.Id).Single().Count++;
                }
                else // add new product
                {
                    userDb.Basket.Where(b => b.StateValue == (int)State.NotOrdered).Single().OrderPosition.Add(new OrderPosition() { Product = productDb, ProductId = productDb.Id, Count = 1 });
                }
            }


        }

        public IQueryable<Entity.Baskets.Basket> GetAllOrdersForUser(string username, Entity.Baskets.State state)
        {
            Data.ModelDatabase.User userDb = context.Users.Where(u => u.Username == username).Single();
            List<Data.ModelDatabase.Basket> basketList = userDb.Basket.Where(b => b.StateValue == (int)state).ToList();
            return mapper.WrapperData(basketList).AsQueryable();
        }


        public IQueryable<Entity.Baskets.Basket> GetAllOrders(Entity.Baskets.State state)
        {
            return mapper.WrapperData(context.Basket.Where(x => x.StateValue == (int)state).ToList()).AsQueryable();
        }


        public void DeleteOrderPosition(int IdOrderPosition)
        {
            OrderPosition entity = context.OrderPosition.Find(IdOrderPosition);
            context.OrderPosition.Remove(entity);
        }

        public void SubmitOrder(string username)
        {
            
            Data.ModelDatabase.User userDb = context.Users.Where(u => u.Username == username).Single();
            if (userDb.Basket.Where(b => b.StateValue == (int)State.NotOrdered).Count() != 0)
            {
                Data.ModelDatabase.Basket basket = userDb.Basket.Where(b => b.StateValue == (int)State.NotOrdered).Single();
                basket.State = State.Ordered;
            }
        }

        public void DeleteBasket(int IdBasket)
        {
            Data.ModelDatabase.Basket basket = context.Basket.Find(IdBasket);
            context.Basket.Remove(basket);
        }


        public decimal GetPrice(string username)
        {
            Data.ModelDatabase.User user = context.Users.Where(x => x.Username == username).Single();

            List<OrderPosition> orderPositions = new List<OrderPosition>();

            if (user.Basket.Where(x => x.State == State.NotOrdered).Count() != 0)
            {
                orderPositions = user.Basket.Where(x => x.State == State.NotOrdered).Single().OrderPosition.ToList();

                decimal sum = 0;
                foreach (OrderPosition p in orderPositions)
                    sum += p.Product.Price * p.Count;
                return sum;
            }
            else
            {
                return 0;
            }
            
        }
    }
}
